package com.university.trade.util;
import freemarker.cache.FileTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URLEncoder;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ExportWordUtil {
	@SuppressWarnings("unused")
	private static final int BUFFER_SIZE = 1024;
	private Configuration configuration = null;
	private Map<String,Object> dataMap;

	public ExportWordUtil() {
		this.configuration = new Configuration();
		this.configuration.setDefaultEncoding("utf-8");
	}

	public File createDoc(Map<String,Object> map, String templateName, String cname,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
		this.dataMap = map;

		TemplateLoader templateLoader = null;
		String path = "";
		Template t = null;
		try {
			String ctxRealPath = request.getSession().getServletContext().getRealPath("/");
			templateLoader = new FileTemplateLoader(new File(ctxRealPath));
			this.configuration.setTemplateLoader(templateLoader);
			path = "/template/" + templateName;
			t = this.configuration.getTemplate(path, "UTF-8");
		} catch (IOException e2) {
			e2.printStackTrace();
		}
		PropertiesUtil systemProperties = new PropertiesUtil("/systemConfig.properties");
		String word_print_temp_path = systemProperties.getProperty("word_print_temp_path");
		File temp = new File(word_print_temp_path);
		if (!temp.exists()) {
			temp.mkdirs();
		}
		File outFile = new File(word_print_temp_path + "/" + cname);
		Writer out = null;
		try {
			FileOutputStream fos = new FileOutputStream(outFile);
			OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
			out = new BufferedWriter(oWriter);
			t.process(this.dataMap, out);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			BufferedInputStream br = new BufferedInputStream(new FileInputStream(outFile));
			byte[] buf = new byte[1024];
			int len = 0;
			response.reset();
			if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0)
				cname = new String(cname.getBytes("UTF-8"), "ISO8859-1");
			else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0)
				cname = URLEncoder.encode(cname, "UTF-8");
			else {
				cname = new String(cname.getBytes("UTF-8"), "ISO8859-1");
			}

			response.setHeader("Content-Disposition", "attachment; filename=" + cname);
			OutputStream out2 = response.getOutputStream();
			while ((len = br.read(buf)) != -1)
				out2.write(buf, 0, len);
			br.close();
			out2.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Map<String,Object> getDataMap() {
		return this.dataMap;
	}

	public void setDataMap(Map<String,Object> dataMap) {
		this.dataMap = dataMap;
	}
}
